package com.example.trans.controller;

import com.example.trans.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 程序运行正常 - 自动提交
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @RequestMapping("/insert")
    public Boolean insertUserInfo(String userName, String password){
        Integer result = userInfoService.insertUserInfo(userName, password);
        return true;
    }

    /**
     * 程序运行异常 - 自动回滚
     */
    @Transactional
    @RequestMapping("/r1")
    public Boolean r1(String userName, String password){
        Integer result = userInfoService.insertUserInfo(userName, password);
        throw new RuntimeException();
    }

    /**
     * 程序异常，异常捕获，事务提交
     */
    @Transactional
    @RequestMapping("/r2")
    public Boolean r2(String userName, String password){
        Integer result = userInfoService.insertUserInfo(userName, password);
        if(result > 0){
            Integer ret = 10 / 0;
            return true;
        }
        return false;
    }

    /**
     * 程序异常，异常捕获，自动提交
     */
    @Transactional
    @RequestMapping("/r3")
    public Boolean r3(String userName, String password){
        try{
            Integer result = userInfoService.insertUserInfo(userName, password);
            if(result > 0){
                Integer ret = 10 / 0;
                return true;
            }
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 程序异常，捕获异常，重新抛出异常，实现回滚
     */
    @Transactional
    @RequestMapping("/r4")
    public Boolean r4(String userName, String password){
        try{
            Integer result = userInfoService.insertUserInfo(userName, password);
            if(result > 0){
                Integer ret = 10 / 0;
                return true;
            }
            return false;
        }catch (Exception e){
            throw e;
        }
    }

    /**
     * 程序异常，捕获异常，手动实现回滚
     */
    @Transactional
    @RequestMapping("/r5")
    public Boolean r5(String userName, String password){
        try{
            Integer result = userInfoService.insertUserInfo(userName, password);
            if(result > 0){
                Integer ret = 10 / 0;
                return true;
            }
            return false;
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    /**
     * 程序存在受查异常，自动提交
     */
    @Transactional
    @RequestMapping("/r6")
    public Boolean r6(String userName, String password) throws IOException {
        Integer result = userInfoService.insertUserInfo(userName, password);
        if(result > 0){
            throw new IOException();
        }
        return false;
    }

    /**
     * 程序存在受查异常，通过设置rollbackFor，实现自动回滚
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r7")
    public Boolean r7(String userName, String password) throws IOException {
        Integer result = userInfoService.insertUserInfo(userName, password);
        if(result > 0){
            throw new IOException();
        }
        return false;
    }
}
